//package com.ketangpai.admin.controller;
//
//import com.ketangpai.admin.common.PageResult;
//import com.ketangpai.admin.common.Result;
//import com.ketangpai.admin.dto.WorkDTO;
//import com.ketangpai.admin.service.WorkService;
//import com.ketangpai.admin.utils.JsonResult;
//import com.ketangpai.admin.vo.WorkVO;
//import io.swagger.annotations.ApiOperation;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.*;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.List;
//import java.util.UUID;
//
//
//@RestController
//@RequestMapping("/api/work")
//@Slf4j
//public class WorkController {
//
//    @Autowired
//    private WorkService workService;
//    private final String uploadPath = "C:/XS1/";
//    @GetMapping("/{id}")
//    public Result<WorkVO> getWorkById(@PathVariable Integer id) {
//        return Result.success(workService.getWorkById(id));
//    }
//
//    @GetMapping("/list")
//    public Result<PageResult<WorkVO>> getWorkList(
//            @RequestParam Integer courseId,
//            @RequestParam(required = false) Integer status,
//            @RequestParam(defaultValue = "1") Integer pageNum,
//            @RequestParam(defaultValue = "10") Integer pageSize) {
//        return Result.success(workService.getWorkList(courseId, status, pageNum, pageSize));
//    }
//
//    @PostMapping("/insert")
//    public Result<Void> createWork(@RequestBody WorkDTO workDTO) {
//        workService.createWork(workDTO);
//        return Result.success();
//    }
//
//    @PutMapping("/update/{id}")  // 添加 id 路径参数
//    public Result<Void> updateWork(@PathVariable Integer id, @Validated @RequestBody WorkDTO workDTO) {
//        workDTO.setId(id);  // 确保 DTO 中设置了正确的 id
//        workService.updateWork(workDTO);
//        return Result.success();
//    }
//    @ApiOperation("通过课程ID查询作业")
//    @GetMapping("/queryByCid/{id}")
//    public JsonResult<?> queryByCid(@PathVariable Integer id) {
//        List<com.ketangpai.admin.entity.dto.WorkDTO> works = workService.queryByCid(id);
//        return JsonResult.success(works);
//    }
//    @DeleteMapping("/{id}")
//    public Result<Void> deleteWork(@PathVariable Integer id) {
//        workService.deleteWork(id);
//        return Result.success();
//    }
//
//    @PutMapping("/release/{id}")
//    public Result<Void> releaseWork(@PathVariable Integer id) {
//        workService.releaseWork(id);
//        return Result.success();
//    }
//
//    // WorkController.java
//
//    @PostMapping("/upload/{id}")
//    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
//        try {
//            // 确保目录存在
//            File dir = new File(uploadPath);
//            if (!dir.exists()) {
//                dir.mkdirs();
//            }
//            // 生成唯一文件名
//            String originalFilename = file.getOriginalFilename();
//            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//            String filename = UUID.randomUUID().toString() + extension;
//
//            // 完整的文件路径
//            String filePath = uploadPath + filename;
//
//            // 保存文件
//            File dest = new File(filePath);
//            file.transferTo(dest);
//
//            // 返回文件名（不包含路径）
//            return Result.success(filename);
//        } catch (IOException e) {
//            log.error("文件上传失败", e);
//            return Result.error("文件上传失败");
//        }
//    }
//
//
//}
package com.ketangpai.admin.controller;

import com.ketangpai.admin.common.PageResult;
import com.ketangpai.admin.common.Result;
import com.ketangpai.admin.dto.WorkDTO;
import com.ketangpai.admin.service.WorkService;
import com.ketangpai.admin.utils.JsonResult;
import com.ketangpai.admin.vo.WorkVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/api/work")
@Slf4j
public class WorkController {

    @Autowired
    private WorkService workService;
    private final String uploadPath = "C:/XS1/";
    @GetMapping("/{id}")
    public Result<WorkVO> getWorkById(@PathVariable Integer id) {
        return Result.success(workService.getWorkById(id));
    }

    @GetMapping("/list")
    public Result<PageResult<WorkVO>> getWorkList(
            @RequestParam Integer courseId,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(workService.getWorkList(courseId, status, pageNum, pageSize));
    }

    @PostMapping("/insert")
    public Result<Void> createWork(@RequestBody WorkDTO workDTO) {
        workService.createWork(workDTO);
        return Result.success();
    }

    @PutMapping("/update/{id}")  // 添加 id 路径参数
    public Result<Void> updateWork(@PathVariable Integer id, @Validated @RequestBody WorkDTO workDTO) {
        workDTO.setId(id);  // 确保 DTO 中设置了正确的 id
        workService.updateWork(workDTO);
        return Result.success();
    }
    @ApiOperation("通过课程ID查询作业")
    @GetMapping("/queryByCid/{id}")
    public JsonResult<?> queryByCid(@PathVariable Integer id) {
        List<com.ketangpai.admin.entity.dto.WorkDTO> works = workService.queryByCid(id);
        return JsonResult.success(works);
    }
    @DeleteMapping("/{id}")
    public Result<Void> deleteWork(@PathVariable Integer id) {
        workService.deleteWork(id);
        return Result.success();
    }

    @PutMapping("/release/{id}")
    public Result<Void> releaseWork(@PathVariable Integer id) {
        workService.releaseWork(id);
        return Result.success();
    }

    // WorkController.java

    @PostMapping("/upload/{id}")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // 确保目录存在
            File dir = new File(uploadPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String filename = UUID.randomUUID().toString() + extension;

            // 完整的文件路径
            String filePath = uploadPath + filename;

            // 保存文件
            File dest = new File(filePath);
            file.transferTo(dest);

            // 返回文件名（不包含路径）
            return Result.success(filename);
        } catch (IOException e) {
            log.error("文件上传失败", e);
            return Result.error("文件上传失败");
        }
    }
}

